// Index into the light textures
uniform float lightScale;

// Deferred shader textures
samplerRect dsEC;
samplerRect dsNorm;
samplerRect dsColor;

void main (void)
{
	// TODO add ambient?, emmissive?, specular
	vec4 final;
	
	vec3 n = normalize(textureRect(dsNorm, gl_TexCoord[0]));
	vec3 position = textureRect(dsEC, gl_TexCoord[0]);
	vec4 diffuse = textureRect(dsColor, gl_TexCoord[0]);
	
	vec3 lightPosition = gl_LightSource[0].position;
	vec4 lightDiffuse = gl_LightSource[0].diffuse;
	vec3 l = normalize(lightPosition - position);
	
	float nDotL= max(dot(n,l),0);
	float att = length(lightPosition - position);

	final += lightDiffuse * 
		diffuse * 
		nDotL;	
//	final /= (1*att);
/*
	vec3 E = normalize(eyeVec);
	vec3 R = reflect(-L, N);
	float specular = pow( max(dot(R, E), 0.0), 
			gl_FrontMaterial.shininess );
	final_color += gl_LightSource[0].specular * 
		gl_FrontMaterial.specular * 
		specular;	
*/
	gl_FragColor = final * lightScale;
}

